package com.application.Activity;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.application.Adapter.ListSubjectAdapter;
import com.application.Adapter.ListSubjectAdapter.ViewSubject;
import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.DataProvider.SubjectDAO;

public class FrmSubject extends Activity implements OnItemClickListener{
	public static int ON_EXIT_APP=1111;
	static ListView _listSubject;	
	private static ArrayList<ViewSubject> _arrayWork;
	private static ListSubjectAdapter _arrayAdapter;
	private static boolean _isWait = false; // biến lữu trữ xem có chờ load hay ko?
	private static ProgressDialog _dialog;
	private static Context _cont;
	private static ArrayList<ArrayList<String>> info_subject_teacher = new ArrayList<ArrayList<String>>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_subject);		

		_cont = this;
		_listSubject = (ListView)findViewById(R.id.frmsubject_lvListSubject) ;
		_arrayWork = new ArrayList<ViewSubject>();		
//		TextView tvUserName = (TextView)findViewById(R.id.subject_tvUsername);
//		tvUserName.setText(NetworkController.get_userName());
//		tvUserName.setOnClickListener(new OnClickListener() {

//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				Intent iIntent = new Intent(FrmSubject.this,FrmConversation.class);
//				startActivity(iIntent);
//			}
//		});
		//		if(_isWait = true)
		//		{
		//			set_dialog(ProgressDialog.show(FrmSubject.this, "","Loading ...", true));
		//
		//		}
		ShowSubjectList();		
		_listSubject.setOnItemClickListener( this);
	}


	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
//		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setIcon(R.drawable.img_dialog);
//			builder.setMessage("Bạn có chắc muốn thoát khỏi chương trình ?")
//			.setCancelable(false)
//			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//				@Override
//				public void onClick(DialogInterface dialog, int id) {
//					if(!NetworkController.isOffline())
//					{
//						String s = NetworkController.get_ipHost();
//						NetworkController.SendMessageLogout(new Address(s));
//						NetworkController.peerRelease();
//						Log.d("PEERDROID","tat peer");
//					}
//						//NetworkController.SendMessageLogout(new Address("192.168.173.20:5033"));
//						Log.d("PEERDROID","Đã Thoát khỏi chương trình");
//						finish();
//						System.exit(0);
//				}
//			})
//			.setNegativeButton("No", new DialogInterface.OnClickListener() {
//				@Override
//				public void onClick(DialogInterface dialog, int id) {
//					dialog.cancel();
//				}
//			});
//			AlertDialog alert = builder.create();
//			alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 255, 255, 255))); 
//			alert.show();
//			return true;
//		}
//
		return super.onKeyDown(keyCode, event);	
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("Phu","on onActivityResult nhom cho");
		 if (resultCode == ON_EXIT_APP) {
			 Intent i=new Intent(this, FrmTabActivity.class);
			// startActivityForResult(i, ON_EXIT_APP);
			 setResult(ON_EXIT_APP);
			 finish();
		 }
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("PEERDROID","vao onstart subject");
	//	ActivityController.setPeerActivity(this);
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d("PEERDROID", "vao restart");
	//	ActivityController.setPeerActivity(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
	//	ActivityController.setPeerActivity(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	//	ActivityController.setPeerActivity(this);
		Log.d("PEERDROID", "vao pause");
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//		FrmLogin._peer.halt();
	}

	public static void updateListAdapter()
	{
		info_subject_teacher.clear();
		//_arrayWork.clear();
		_arrayWork = new ArrayList<ListSubjectAdapter.ViewSubject>();
		SubjectDAO sjDAO = new SubjectDAO(_cont);
		info_subject_teacher = sjDAO.getSubject_Class(NetworkController.get_idClass());
		Log.d("PEERDROID", "info_subject_teacher: " + info_subject_teacher.toString());
		try {
			int n = info_subject_teacher.size();
			if(n != 0)
			{
				for(int i = 0; i < n; i++)
				{
					boolean status = sjDAO.getStatus(Integer.valueOf(info_subject_teacher.get(i).get(2)));
					ViewSubject view = new ViewSubject(info_subject_teacher.get(i).get(1), info_subject_teacher.get(i).get(0), Integer.valueOf(info_subject_teacher.get(i).get(2)),status);
					_arrayWork.add(view);
				}
			}
			Log.d("PEERDROID", "arrayWork size: " + _arrayWork.size());
			Log.d("PEERDROID", "arrayWork: " + _arrayWork);
			setListAdapter();
		} catch (android.database.SQLException e) {
			Log.d("PEERDROID", "Query error!");
		}
	}
	public static void setListAdapter() {
		_arrayAdapter=new ListSubjectAdapter(_cont,R.layout.listrow_listsubject,_arrayWork);
		_listSubject.setAdapter(_arrayAdapter);
	}
	//Hien thi danh sach mon hoc
	private void ShowSubjectList(){	
		
		Log.d("PEERDROID", "vao show subjectlist");
		if(info_subject_teacher.size() != 0)
			info_subject_teacher.clear();
		Log.d("PEERDROID", "ID Class" + NetworkController.get_idClass());
		SubjectDAO sjDAO = new SubjectDAO(null);
		info_subject_teacher = sjDAO.getSubject_Class(NetworkController.get_idClass());
		Log.d("PEERDROID", "subject_teacher" + info_subject_teacher.toString());
		try {
			int n = info_subject_teacher.size();
			if(n != 0)
			{
				for(int i = 0; i < n; i++)
				{
					boolean status = sjDAO.getStatus(Integer.valueOf(info_subject_teacher.get(i).get(2)));
					ViewSubject view = new ViewSubject(info_subject_teacher.get(i).get(1), info_subject_teacher.get(i).get(0), Integer.valueOf(info_subject_teacher.get(i).get(2)),status);
					_arrayWork.add(view);
				}
			}
			setListAdapter();
		} catch (android.database.SQLException e) {
			Log.d("PEERDROID", "Query error!");
		}
	}

	//--- Xu ly su kien ItemClick tren listview
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		// TODO Auto-generated method stub
		//Log.d("PEERDROID","Da vao ham Onclick");
		showFrmChapter(pos);		
	}

	private void showFrmChapter(int pos) {

		Log.d("PEERDROID","Chuan bi Show Subject");

		String idSub =info_subject_teacher.get(pos).get(2);
		SubjectDAO sjDao = new SubjectDAO(null);
		sjDao.updateStatusSubject(false, Integer.valueOf(idSub));
		//Send id
		Intent iIntent = new Intent(FrmSubject.this,FrmChapter.class);
		Bundle bBun = new Bundle();
		bBun.putString("id", idSub);
		iIntent.putExtras(bBun);
		StackActivity activityStack = (StackActivity) getParent();
	    activityStack.push("FrmChapter", iIntent);

		Log.d("PEERDROID","Send id Subject và Start Chapter");		

	}

	public static void set_isWait(boolean _isWait) {
		FrmSubject._isWait = _isWait;
	}

	public static boolean is_isWait() {
		return _isWait;
	}

	public static void set_dialog(ProgressDialog _dialog) {
		FrmSubject._dialog = _dialog;
	}

	public static ProgressDialog get_dialog() {
		return _dialog;
	}

}






















